// src/pages/dataManager/index.tsx
import { Flex } from 'antd';
import { useEffect, useState } from 'react';
import { useNavigate } from 'react-router-dom';
import { Outlet, useLocation } from 'umi';

import SideBar from './Sidebar';
import styles from './index.less';

const DataManager = () => {
  const location = useLocation();

  const [isLoading, setIsLoading] = useState(false);
  const navigate = useNavigate();

  useEffect(() => {
    if (location.pathname === '/dataManager') {
      setIsLoading(true);
      // 模拟加载延迟
      const timer = setTimeout(() => {
        navigate('/dataManager/RiceVarietiesList', { replace: true });
        setIsLoading(false);
      }, 100);
      return () => clearTimeout(timer);
    }
  }, [location.pathname]);

  if (isLoading) return <div>加载中...</div>;
  // 确保根路径也高亮RiceVarietiesList
  const selectedKey =
    location.pathname === '/dataManager'
      ? '/dataManager/RiceVarietiesList'
      : location.pathname;

  return (
    <Flex className={styles.wrapper}>
      <SideBar selectedKey={selectedKey} />
      <Flex flex={1}>
        <Outlet />
      </Flex>
    </Flex>
  );
};

export default DataManager;
